package cn.tedu.db.dao.Impl;

import cn.tedu.db.dao.UserDao;
import cn.tedu.db.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

// @Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public int addUser(User user) {
        String sql = "insert into user (username, password) values (?,?)";

        try(Connection conn = dataSource.getConnection()) {
            PreparedStatement ps =  conn.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            int i = ps.executeUpdate();
            ResultSet rs = ps.getGeneratedKeys();
            while (rs.next()){
                user.setId(rs.getInt(1));
            }
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public int updateUser(User user) {
        String sql = "update user set username=?, password=? where id=?";
        try(Connection conn = dataSource.getConnection()) {
            PreparedStatement ps =  conn.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setInt(3,user.getId());
            int i = ps.executeUpdate();
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public int deleteUser(Integer id) {
        String sql = "delete from user where id=?";
        try(Connection conn = dataSource.getConnection()) {
            PreparedStatement ps =  conn.prepareStatement(sql);
            ps.setInt(1,id);
            int i = ps.executeUpdate();
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<User> findAllUser() {
        List<User> list = new ArrayList<>();
        String sql = "select * from user";
        try (Connection conn = dataSource.getConnection()){
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("username");
                String pwd = rs.getString("password");
                User user = new User(id,name,pwd);
                list.add(user);
            }
            return list;
        }catch (SQLException e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public User findUserById(Integer id) {
        User user = null;
        String sql = "select * from user where id=?";
        try (Connection conn = dataSource.getConnection()){
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                id = rs.getInt("id");
                String name = rs.getString("username");
                String pwd = rs.getString("password");
                user = new User(id,name,pwd);
            }
            return user;
        }catch (SQLException e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public User findUserByName(String username) {
        User user = null;
        String sql = "select * from user where username=?";
        try (Connection conn = dataSource.getConnection()){
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("username");
                String pwd = rs.getString("password");
                user = new User(id,name,pwd);
            }
            return user;
        }catch (SQLException e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public int countUser() {
        String sql = "select count(*) from user";
        try(Connection connection = dataSource.getConnection()){
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            int num = 0;
            while (rs.next()){
                num = rs.getInt(1);
            }
            return num;
        }catch (SQLException e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Map<String, Object>> findAllUserInfo() {
        return null;
    }
}









